package cn.bupt.adt.controller.inf;

import cn.bupt.adt.entity.autogenerate.AdminUser;
import cn.bupt.adt.entity.autogenerate.Carousel;
import cn.bupt.adt.service.IAdminUserService;
import cn.bupt.adt.service.ICarouselService;
import cn.bupt.adt.util.ImageUtil;
import cn.bupt.adt.util.NetDataAccessUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/interface/carousel")
public class CarouselAppController {
    @Autowired
    ICarouselService carouselService;

    @Autowired
    IAdminUserService adminUserService;

    @RequestMapping("/getAllCarousel")
    @ResponseBody
    public Object getAllCarousel(HttpServletRequest request) {
        NetDataAccessUtil nau = new NetDataAccessUtil();
        List<Carousel> carousels = carouselService.getAllCarousels();
        List<String> carouselAddr = new ArrayList<String>(carousels.size());
        for (Carousel c : carousels)
            carouselAddr.add(ImageUtil.contructFullImagePath(c.getSrc(), request));
        nau.setContent(carouselAddr);
        nau.setResult(1);
        nau.setResultDesp("获取轮播图成功");
        return nau;
    }

    @RequestMapping("/getAdminUserDetail")
    @ResponseBody
    public Object adminUserDetail() {
        NetDataAccessUtil nau = new NetDataAccessUtil();
        AdminUser adminUser = adminUserService.getAllAdminUser().get(0);
        Map<String, Object> content = new HashMap<>();
        if (adminUser != null) {
            content.put("Id", adminUser.getId());
            content.put("name", adminUser.getName());
            content.put("phone", adminUser.getPhone());
            content.put("studio", adminUser.getStudio());
            nau.setContent(content);
            nau.setResult(1);
            nau.setResultDesp("获取管理员信息成功");
        } else {
            nau.setContent(null);
            nau.setResult(0);
            nau.setResultDesp("获取管理员信息失败");
        }
        return nau;
    }

}
